package com.guit.hrms.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.guit.hrms.mapper.*;
import com.guit.hrms.po.Employee;
import com.guit.hrms.po.Role;
import com.guit.hrms.po.utilsPO.PageBean;
import com.guit.hrms.service.QueryService;
import com.guit.hrms.vo.EmployeeVO;
import com.guit.hrms.vo.TransactionVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class QueryServiceImpl implements QueryService {

    @Autowired
    private QueryMapper queryMapper;
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private AcademyMapper academyMapper;
    @Autowired
    private OtherDeptMapper otherDeptMapper;
    @Autowired
    private PositionMapper positionMapper;

    /**
     * 查询所有员工
     * @return 所有员工信息
     */
    @Override
    public PageBean query(Integer page, Integer pageSize, Integer department, Integer deptType, Integer position) {
        PageHelper.startPage(page, pageSize);
        List<EmployeeVO> employeeList = queryMapper.query(department, deptType, position);
        List<EmployeeVO> employeeVOList = new ArrayList<>();
        String deptName = null;
        String role = null;
        String positionName = null;
//        EmployeeVO employeeVO;
        for (EmployeeVO employee : employeeList) {
//            employeeVO = new EmployeeVO();
            deptName = getDepartment(employee);
            role = getRoleById(employee.getEmployeeId()).getName();
//            BeanUtils.copyProperties(employee, employeeVO);
            positionName = positionMapper.selectPositionById(Integer.parseInt(employee.getPosition())).getPositionName();
            employee.setDeptName(deptName);
            employee.setRole(role);
            employee.setPosition(positionName);
            employeeVOList.add(employee);
        }
        BeanUtils.copyProperties(employeeVOList, employeeList);
        Page<EmployeeVO> p = (Page<EmployeeVO>) employeeList;
        return new PageBean(p.getTotal(), p.getResult());
    }

    /**
     * 获取单个员工信息
     * @param employeeId 员工号
     * @return 单个员工信息
     */
    @Override
    public EmployeeVO getById(String employeeId) {
        Employee employee = queryMapper.getById(employeeId);
        String deptName = null;
        if (employee.getDeptType() == 1){
            deptName = departmentMapper.selectDepartmentById(employee.getDepartment()).getDeptName();
        } else if (employee.getDeptType() == 2) {
            deptName = academyMapper.getAcademyById(employee.getDepartment()).getAcademyName();
        } else if (employee.getDeptType() == 3) {
            deptName = otherDeptMapper.getOtherDeptById(employee.getDepartment()).getOtherDeptName();
        }
        String positionName = positionMapper.selectPositionById(employee.getPosition()).getPositionName();
        EmployeeVO employeeVO = new EmployeeVO();
        BeanUtils.copyProperties(employee,employeeVO);
        employeeVO.setDeptName(deptName);
        employeeVO.setPosition(positionName);
        return employeeVO;
    }

    @Override
    public Role getRoleById(String employeeId) {
        return queryMapper.getRoleById(employeeId);
    }

    @Override
    public String getDepartment(EmployeeVO employee) {
        String deptName = null;
        if (employee.getDeptType() == 1){
            deptName = departmentMapper.selectDepartmentById(employee.getDepartment()).getDeptName();
        } else if (employee.getDeptType() == 2) {
            deptName = academyMapper.getAcademyById(employee.getDepartment()).getAcademyName();
        } else if (employee.getDeptType() == 3) {
            deptName = otherDeptMapper.getOtherDeptById(employee.getDepartment()).getOtherDeptName();
        }
        return deptName;
    }
}
